﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WWWSiteHelper
{
    public class TagAttributeKnowledge
    {
        public class BasicTag
        {
            public BasicTag(string name, bool selfClosing)
            {
                this.Name = name;
                this.SelfClosing = selfClosing;
            }

            public string Name { get; private set; }
            public bool SelfClosing { get; private set; }
        }
        private ICollection<BasicTag> tags = new List<BasicTag>();
        private IDictionary<string, ICollection<string>> attributes = new Dictionary<string, ICollection<string>>();

        public TagAttributeKnowledge(TagAttributeCountingKnowledge counted)
        {
            foreach (KeyValuePair<string, TagAttributeCountingKnowledge.TagTraitCount> tag in counted.getTags())
            {
                bool selfClosing = (tag.Value.Closed == 0);
                tags.Add(new BasicTag(tag.Key, selfClosing));
                attributes[tag.Key] = counted.getAttributes(tag.Key).Keys;
            }
        }

        public static TagAttributeKnowledge parse(string content)
        {
            //TagAttributeKnowledge result = new TagAttributeKnowledge();
            TagAttributeCountingKnowledge counted = TagAttributeCountingKnowledge.parse(content);
            return new TagAttributeKnowledge(counted);
        }

        public List<BasicTag> getTags()
        {
            return new List<BasicTag>(tags);
        }

        public SortedSet<string> getAttributes(string tag)
        {
            if (attributes.ContainsKey(tag))
            {
                return new SortedSet<string>(attributes[tag]);
            }
            return new SortedSet<string>();
        }
    }
}
